package com.qiantu.phone.ui.activity;

import android.content.Intent;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;

import androidx.annotation.Nullable;

import com.hjq.http.listener.HttpCallback;
import com.hjq.toast.ToastUtils;
import com.hjq.widget.layout.SettingBar;
import com.qiantu.api.db.LLHouseDBManager;
import com.qiantu.api.entity.SmsBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class AccountManagerActivity extends AppActivity {

    private com.hjq.widget.layout.SettingBar accountChange;
    private com.hjq.widget.layout.SettingBar changePhone;
    private com.hjq.widget.layout.SettingBar logoff;
    private  SettingBar setPassWord;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_account_manager;
    }

    @Override
    protected void initView() {
        accountChange = (SettingBar) findViewById(R.id.account_change);
        changePhone = (SettingBar) findViewById(R.id.change_phone);
        logoff = (SettingBar) findViewById(R.id.logoff);
        setPassWord = findViewById(R.id.set_password);
        SpannableString spannableString = new SpannableString(getString(R.string.logoff));
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(AppApplication.getInstance().getThemeColor(R.attr.secondaryColor1));
        spannableString.setSpan(colorSpan, 2, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        logoff.setLeftText(spannableString);
        setOnClickListener(accountChange, changePhone, logoff, setPassWord);
    }

    @Override
    protected void initData() {
        if (AppApplication.getInstance().getLoginInfo().getUserInfo().getHasPassword()){
            setPassWord.setLeftText(R.string.change_password);
        }else {
            setPassWord.setLeftText(R.string.set_password);
        }
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view == accountChange){
            startActivity(ChangeAccountActivity.class);
        }else if (view == changePhone){
            getSmsCode();
        }else if (view == logoff){
            //用户作为户主的房屋中存在等待被过户的房屋时，该用户无法进行“注销账号”的操作。
            if(LLHouseDBManager.getInstance(this).hasTransferringHouses()){
                ToastUtils.show(R.string.transferring_house_logoff_hint);
                return;
            }
            startActivity(LogOffActivity.class);
        }else if (view == setPassWord){
            if (AppApplication.getInstance().getLoginInfo().getUserInfo().getHasPassword()){
                startActivity(CheckPwdActivity.class);
            }else {
                startActivityForResult(SetPwdActivity.class, new OnActivityCallback() {
                    @Override
                    public void onActivityResult(int resultCode, @Nullable Intent data) {
                        if (resultCode == RESULT_OK){
                            if (AppApplication.getInstance().getLoginInfo().getUserInfo().getHasPassword()){
                                setPassWord.setLeftText(R.string.change_password);
                            }else {
                                setPassWord.setLeftText(R.string.set_password);
                            }
                        }
                    }
                });
            }
        }

    }

    private void getSmsCode() {
        showDialog();
        Map<String, Object> data = new HashMap<>();
        data.put("mobile", AppApplication.getInstance().getUserPhone());
        data.put("smsType", "changeMobile1_boyun");
        // 隐藏软键盘
        hideKeyboard(getCurrentFocus());
        LLHttpManager.sendSms(this, data, new HttpCallback<HttpData<SmsBean>>(this) {
            @Override
            public void onStart(Call call) {
            }

            @Override
            public void onEnd(Call call) {
                hideDialog();
            }

            @Override
            public void onSucceed(HttpData<SmsBean> data) {
                if (data.isRequestSucceed()) {
                    toast(R.string.sms_code_send);
                    postDelayed(() -> {
                        // 跳转到验证码界面
                        //验证老手机号，传如原smsId
                        Captcha1Activity.start(getAppActivity(), AppApplication.getInstance().getUserPhone(), data.getData().getSmsID(), null,2);
                    }, 600);
                }
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }
        });

    }
}